namespace SmartSoft.Lists
{
    using SmartSoft;
    using SmartSoft.Controls;
    using SmartSoft.Controls.ChildGridControls;
    using SmartSoft.ADP;
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;

    public class FormDmHdVtEditor : FormListEditor
    {
        private SearchColumn Column1;
        private DataGridViewTextBoxColumn Column2;
        private DataGridViewTextBoxColumn Column3;
        private NumericColumn Column4;
        private NumericColumn Column5;
        private NumericColumn Column6;
        private IContainer components;
        private GridDetail grdDetail;
        private DmHdDataAccess oMethod = new DmHdDataAccess();
        private DataTable oTable;

        public FormDmHdVtEditor()
        {
            this.InitializeComponent();
        }

        private bool AddNewRow()
        {
            if ((this.grdDetail.CurrentRow == null) || (this.grdDetail.CurrentRow.DataBoundItem == null))
            {
                return false;
            }
            if (((decimal) ((DataRowView) this.grdDetail.CurrentRow.DataBoundItem).Row["So_Luong"]) == 0M)
            {
                return false;
            }
            if (this.grdDetail.CurrentRow.Index != (this.grdDetail.Rows.Count - 1))
            {
                return false;
            }
            this.grdDetail.EndEdit();
            DataRow row = this.oTable.NewRow();
            row["Stt"] = base.LocalCurrentRow[base.LocalFormLister.LocalKeyField];
            row["Stt0"] = SSDataAccess.Cong_So_Tt(AppInstance.Subsidiary);
            row["So_Luong"] = 0;
            row["Gia"] = 0;
            row["Tien"] = 0;
            this.oTable.Rows.Add(row);
            BindingSource dataSource = this.grdDetail.DataSource as BindingSource;
            dataSource.Position = dataSource.Find("Stt0", row["Stt0"]);
            return true;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void grdDetail_CellBottomLeftLeaved(object sender, EventArgs e)
        {
            if (((this.grdDetail.Rows.Count == 1) || !string.IsNullOrEmpty(((DataRowView) this.grdDetail.CurrentRow.DataBoundItem).Row["Ma_Vt"].ToString())) || !string.IsNullOrEmpty(base.ActiveControl.Text))
            {
                this.grdDetail.UserMoveNextCell = GridDetail.MoveNextType.NextCell;
            }
            else
            {
                this.grdDetail.UserMoveNextCell = GridDetail.MoveNextType.DeleteCurrentRow;
            }
        }

        private void grdDetail_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if ((e.ColumnIndex == 3) || (e.ColumnIndex == 4))
            {
                DataRow row = ((DataRowView) this.grdDetail.CurrentRow.DataBoundItem).Row;
                if ((row["So_Luong"] != DBNull.Value) && (row["Gia"] != DBNull.Value))
                {
                    row["Tien"] = ((decimal) row["So_Luong"]) * ((decimal) row["Gia"]);
                    this.grdDetail.Refresh();
                }
            }
        }

        private void grdDetail_CellRightLeaved(object sender, EventArgs e)
        {
            if (this.AddNewRow())
            {
                this.grdDetail.UserMoveNextCell = GridDetail.MoveNextType.FirstCell;
            }
            else
            {
                this.grdDetail.UserMoveNextCell = GridDetail.MoveNextType.FirstCellNextRow;
            }
        }

        private void grdDetail_CurrentRowsRemoved(object sender, EventArgs e)
        {
            this.OnDetailDelete();
        }

        private void InitializeComponent()
        {
            DataGridViewCellStyle style = new DataGridViewCellStyle();
            DataGridViewCellStyle style2 = new DataGridViewCellStyle();
            DataGridViewCellStyle style3 = new DataGridViewCellStyle();
            DataGridViewCellStyle style4 = new DataGridViewCellStyle();
            this.grdDetail = new GridDetail();
            this.Column1 = new SearchColumn();
            this.Column2 = new DataGridViewTextBoxColumn();
            this.Column3 = new DataGridViewTextBoxColumn();
            this.Column4 = new NumericColumn();
            this.Column5 = new NumericColumn();
            this.Column6 = new NumericColumn();
            ((ISupportInitialize) this.grdDetail).BeginInit();
            base.SuspendLayout();
            base.txtMa_Loai3.Location = new Point(0x17b, 0x106);
            base.txtMa_Loai2.Location = new Point(0xfb, 0x106);
            base.txtMa_Loai1.Location = new Point(0x7b, 0x106);
            base.lblPhan_Loai.Location = new Point(12, 0x109);
            this.grdDetail.AllowUserToAddRows = false;
            this.grdDetail.AllowUserToDeleteRows = false;
            this.grdDetail.CheckAutoSize = true;
            this.grdDetail.CheckColor = false;
            this.grdDetail.Columns.AddRange(new DataGridViewColumn[] { this.Column1, this.Column2, this.Column3, this.Column4, this.Column5, this.Column6 });
            this.grdDetail.Dock = DockStyle.Fill;
            this.grdDetail.Font = new Font("Arial", 8.25f);
            this.grdDetail.Location = new Point(0, 0x19);
            this.grdDetail.Name = "grdDetail";
            this.grdDetail.Size = new Size(0x2b6, 0x10f);
            this.grdDetail.TabIndex = 0x27;
            this.grdDetail.CurrentRowsRemoved += new EventHandler(this.grdDetail_CurrentRowsRemoved);
            this.grdDetail.CellRightLeaved += new EventHandler(this.grdDetail_CellRightLeaved);
            this.grdDetail.CellBottomLeftLeaved += new EventHandler(this.grdDetail_CellBottomLeftLeaved);
            this.grdDetail.CellEndEdit += new DataGridViewCellEventHandler(this.grdDetail_CellEndEdit);
            this.Column1.DataPropertyName = "Ma_Vt";
            style.Alignment = DataGridViewContentAlignment.MiddleLeft;
            this.Column1.DefaultCellStyle = style;
            this.Column1.DestinationFields = "Ten_Vt, Dvt";
            this.Column1.DisabledForeColor = Color.Blue;
            this.Column1.FillWeight = 93.08755f;
            this.Column1.HeaderText = "Mã vật tư";
            this.Column1.Name = "Column1";
            this.Column1.SortMode = DataGridViewColumnSortMode.Automatic;
            this.Column1.SourceFields = "Ten_Vt, Dvt";
            this.Column1.TableName = "DMVT";
            this.Column1.UserTrimMode = AppInstance.TextControlTrimMode.All;
            this.Column1.Width = 80;
            this.Column2.DataPropertyName = "Ten_Vt";
            this.Column2.FillWeight = 202.3249f;
            this.Column2.HeaderText = "Tên vật tư";
            this.Column2.Name = "Column2";
            this.Column2.ReadOnly = true;
            this.Column2.Width = 0x123;
            this.Column3.DataPropertyName = "Dvt";
            this.Column3.FillWeight = 39.63169f;
            this.Column3.HeaderText = "Đvt";
            this.Column3.Name = "Column3";
            this.Column3.ReadOnly = true;
            this.Column3.Width = 40;
            this.Column4.DataPropertyName = "So_Luong";
            style2.Alignment = DataGridViewContentAlignment.MiddleRight;
            this.Column4.DefaultCellStyle = style2;
            this.Column4.FillWeight = 72.42487f;
            this.Column4.HeaderText = "Số lượng";
            this.Column4.Name = "Column4";
            this.Column4.SortMode = DataGridViewColumnSortMode.Automatic;
            this.Column4.UserTextNumericFormat = AppInstance.TextNumericFormat.Quantity;
            this.Column4.Width = 70;
            this.Column5.DataPropertyName = "Gia";
            style3.Alignment = DataGridViewContentAlignment.MiddleRight;
            this.Column5.DefaultCellStyle = style3;
            this.Column5.FillWeight = 95.47137f;
            this.Column5.HeaderText = "Đơn giá";
            this.Column5.Name = "Column5";
            this.Column5.SortMode = DataGridViewColumnSortMode.Automatic;
            this.Column5.UserTextNumericFormat = AppInstance.TextNumericFormat.LocalPrice;
            this.Column5.Width = 80;
            this.Column5.ZeroIsValid = true;
            this.Column6.DataPropertyName = "Tien";
            style4.Alignment = DataGridViewContentAlignment.MiddleRight;
            this.Column6.DefaultCellStyle = style4;
            this.Column6.FillWeight = 97.05963f;
            this.Column6.HeaderText = "Thành tiền";
            this.Column6.Name = "Column6";
            this.Column6.ReadOnly = true;
            this.Column6.SortMode = DataGridViewColumnSortMode.Automatic;
            this.Column6.UserTextNumericFormat = AppInstance.TextNumericFormat.LocalAmount;
            this.Column6.Width = 90;
            base.AddTitleText = "Chi tiết mặt hàng";
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.ClientSize = new Size(0x2b6, 0x128);
            base.Controls.Add(this.grdDetail);
            base.EditTitleText = "Chi tiết mặt hàng";
            base.Name = "FormDmHdVtEditor";
            base.ShowAddButton = false;
            base.ShowFreeFields = false;
            this.Text = "Chi tiết mặt hàng";
            base.Controls.SetChildIndex(base.lblPhan_Loai, 0);
            base.Controls.SetChildIndex(base.txtMa_Loai1, 0);
            base.Controls.SetChildIndex(base.txtMa_Loai2, 0);
            base.Controls.SetChildIndex(base.txtMa_Loai3, 0);
            base.Controls.SetChildIndex(this.grdDetail, 0);
            ((ISupportInitialize) this.grdDetail).EndInit();
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        public void OnDetailDelete()
        {
            if (MessageBox.Show(AppInstance.DeleMessage, AppInstance.SoftwareName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                this.grdDetail.UserMoveNextCell = GridDetail.MoveNextType.DeleteCurrentRow;
            }
        }

        public override bool OnSave()
        {
            this.oMethod.SaveDetailTable(base.LocalCurrentRow[base.LocalFormLister.LocalKeyField], this.oTable);
            return true;
        }

        public override void OnShow()
        {
            this.oTable = this.oMethod.GetDetailTable(base.LocalCurrentRow[base.LocalFormLister.LocalKeyField].ToString());
            this.grdDetail.DataSource = new BindingSource(this.oTable, this.oTable.TableName);
            if (this.oTable.Rows.Count == 0)
            {
                DataRow row = this.oTable.NewRow();
                row["Stt0"] = SSDataAccess.Cong_So_Tt(AppInstance.Subsidiary);
                row["So_Luong"] = 0;
                row["Gia"] = 0;
                row["Tien"] = 0;
                this.oTable.Rows.Add(row);
            }
        }
    }
}

